package com.cbh.binlog.common;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * LRU - 内存淘汰模型
 * tips: 容量达到限制时, 淘汰最久未访问的元素
 *
 * @author ChenBiHui
 * @date 2025/1/26
 */
public class LRU<K, V> extends LinkedHashMap<K, V> {

    private static final long serialVersionUID = -8829748738083944501L;
    private final int limit;

    public LRU(int limit) {
        super(100, 0.75f, true);
        this.limit = limit;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > this.limit;
    }
}